package spynx.desl.runtime.operators.impl;

import spynx.desl.runtime.IExecutionContext;
import spynx.desl.runtime.IOperator;

public class NumericOperator implements IOperator {

	@Override
	public String getOperator() {
		return "NumOp";
	}

	@Override
	public Object apply(IExecutionContext context, String operator, Object... args) {
		Object op1 = context.resolveValue(args[0]);
		Object op2 = context.resolveValue(args[1]);
		if (!(op1 instanceof Integer) ||
			!(op2 instanceof Integer)) 
			throw new RuntimeException(String.format("Operator %s is not applicable between type %s and %s",
					operator, op1.getClass().getName(), op2.getClass().getName()));
		Integer i1 = (Integer)op1;
		Integer i2 = (Integer)op2;
		
		switch (operator.charAt(0)) {
		case '-': return i1 - i2;
		case '*': return i1 * i2;
		case '/': return i1 / i2;
		case '>': return i1 >> i2;
		case '<': return i1 << i2;
		case '%': return i1 % i2;
		case '&': return i1 & i2;
		case '|': return i1 | i2;
		//case '~': return i1 xor i2;
		}
		return null;
	}

}
